﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Wpf_VirtualRouterPlus.Classes
{
    class WindowsUtils
    {
        public static void addStartUpKey()
        {

            try
            {
                RegistryKey registryKey = Registry.CurrentUser.OpenSubKey
                (@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                if(!checkKey(registryKey, Application.ProductName))
                    registryKey.SetValue(Application.ProductName, Application.ExecutablePath);
            }
            catch (Exception ex)
            {

            }
            
        }

        public static void removeStartUpKey()
        {
            try
            {
                RegistryKey registryKey = Registry.CurrentUser.OpenSubKey
                (@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                if (checkKey(registryKey, Application.ProductName))
                    registryKey.DeleteValue(Application.ProductName);
            }
            catch (Exception ex)
            {

            }
        }

        public static bool checkKey(RegistryKey registryKey_, String name_)
        {
            try
            {
                var value = registryKey_.GetValue(name_);
                if (value != null)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }

            
        }
    }
}
